import React, { Component } from 'react'
import logo from 'assets/logo.png'
import styles from './index.module.scss'
import { Form, Input, Checkbox, Button, Card, message } from 'antd'
import { login } from 'api/user'
import { setToken } from 'utils/storage'
export default class Login extends Component {
  state = {
    isLoginIng: false,
  }
  render() {
    return (
      <div className={styles.login}>
        <Card className="login-container">
          <img className="login-logo" src={logo} alt="" />

          <Form
            name="basic"
            autoComplete="off"
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                { required: true, message: '请输入手机号' },
                {
                  pattern:
                    /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
                  message: '手机号格式不对',
                },
              ]}
            >
              <Input placeholder="请输入手机号" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                { required: true, message: '请输入验证码' },
                { pattern: /^\d{6}$/, message: '验证码格式不对' },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator: (_, value) => {
                    if (value) {
                      // 通过了校验，选中了复选框
                      return Promise.resolve()
                    } else {
                      // 没有通过校验，没选中
                      return Promise.reject(new Error('请同意用户许可'))
                    }
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
  onFinish = async (data) => {
    if (!this.state.isLoginIng) {
      this.setState({
        isLoginIng: true,
      })

      try {
        message.loading({ content: '加载中...', key: 'key' })
        const {
          data: { token },
        } = await login(data)
        // 将token存到本地
        setToken(token)
        // 成功提示
        message.success({ content: '登录成功', key: 'key' })
        // 页面已经调走了，你还在修改原来页面的数据(修改要放在跳转的前面)
        this.setState({
          isLoginIng: false,
        })
        // 跳转到首页
        const pathname = this.props.location?.state?.pathname
        this.props.history.push(pathname || '/home')
      } catch (err) {
        message.error(err.response.data.message)
        this.setState({
          isLoginIng: false,
        })
      }
    }
  }
}
